/**
 * 公共弹窗
 * Name = alert
 * DateTime = Sat Jan 15 2022 23:05:52 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = alert.ts
 * gitee： https://gitee.com/coyeking
 */
 
import { _decorator, Component, Node, Label, Button, Color, Vec3, tween} from 'cc';
const { ccclass, property } = _decorator;
import { audioManager } from '../../framework/audioManager';
import { ModeluType } from '../../define/ccenum';
 
@ccclass('alert')
export class alert extends Component {
    
    @property(Node)
    content: Node = undefined;

    @property({
        type: Label,
        displayName:'提示语',
        tooltip: '提示语'
    })
    label: Label = null;
    
    @property({
        type: Button,
        displayName:'确认按钮',
        tooltip: '确认按钮'
    })
    okBtn: Button = null;
    
    @property({
        type: Button,
        displayName:'取消按钮',
        tooltip: '取消按钮'
    })
    cancelBtn: Button = null;
    
    @property({
        type: Node,
        displayName:'中心节点',
        tooltip: '中心节点'
    })
    centerNode: Node = null;

    _onComplete:any = null;
    _onIOError:any = null;

    /**
     * alert初始化
     * @param msg 
     * @param param 
     * @param type 
     * @returns 
     */
    public init(msg:string,param?: any,type:ModeluType = ModeluType.DEFAULT){
        this.label.string = msg;
        
        let color: Color = new Color().fromHEX("#91512B");
        if (type == ModeluType.SUCCESS){
            color = new Color().fromHEX("#2B914C");
        }
        
        if (type == ModeluType.FAIL){
            color = new Color().fromHEX("#979797");
        }

        this.label.color = color;
        if (!param){
            this.cancelBtn.node.active = false;
            this.okBtn.node.setPosition(this.centerNode.getPosition());
            return;
        }

        this._onComplete = param.success|| null;
        this._onIOError = param.fail|| null;

        this.openAnim();
    }
    
    public onClickSure(){
        audioManager.instance.playSound();
        this._onComplete&&this._onComplete()
        this.closeAction();
    }
    
    public onClickCancel(){
        audioManager.instance.playSound();
        this._onIOError&&this._onIOError();
        this.closeAction();
    }
    
    protected closeAction(cb?: Function, isAct: boolean = true) {
        if (isAct) {
            let animTime = 0.3;
            tween(this.content).to(animTime, { scale: new Vec3() }, { easing: 'backIn' }).call(() => {
                this.node.removeFromParent();
                if (cb) cb();
            }).start();
        }
        else {
            this.node.removeFromParent();
        }
    }

    protected openAnim(cb?: Function, isAct: boolean = true) {
        this.node.active = true;
        let animTime = 0.3;
        if (isAct) {
            this.content.scale = new Vec3();
            tween(this.content).to(animTime, { scale: new Vec3(1, 1, 1) }, { easing: 'backOut' }).call(() => {
                if (cb) cb();
            }).start();
        }
        else {
            this.content.scale = new Vec3(1, 1, 1);
        }
    }
}

